package com.giveu.signature.common.document.pdf.aspose;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;

import java.io.*;


/**
 * doc转PDF
 * @author Masai
 * @date 2019-03-11
 */

class PdfAsposeUtil {

    private static boolean getLicense() {
        boolean result = false;
        InputStream license = null;
        try {
        	ClassLoader loader = Thread.currentThread().getContextClassLoader();
			license = loader.getResourceAsStream("aspose/license.xml");// 凭证文件
            License aposeLic = new License();
            aposeLic.setLicense(license);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
        	try {
				license.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
        return result;
    }

    static void doc2pdf(String inPath,String outPath) throws Exception {
        if (!getLicense()) {
            throw new Exception("com.aspose.words license ERROR!");
        }
        FileOutputStream os = null;
        Document doc = null;
        try {
            File file = new File(outPath);
            os = new FileOutputStream(file);
            doc = new Document(inPath);
            doc.save(os, SaveFormat.PDF);
           
        } catch (Exception e) {
            throw e;
        }finally {
        	 try {
                 os.close();
             } catch (Exception e) {
                 e.printStackTrace();
             }
        	 try {
        		 doc.cleanup();
             } catch (Exception e) {
                 e.printStackTrace();
             }
		}
    }
    
    public static void main(String[] args) throws Exception {
    	doc2pdf("D:\\data\\代扣服务授权书.docx", "D:\\data\\pd.pdf");
	}

}
